﻿function addNewTask(forma){
	var els = forma.elements;
	
	if(!els["komitent_id"].value){
		alert("Morate odabrati komitenta!");
		return;
	}
	
	var out = "a=addNew";
	for(var i=0; i<els.length; i++){
		if(els[i].name){
			//out += els[i].name+": "+els[i].value+"\n";
			out += "&"+els[i].name+"="+escape(trim(els[i].value));
		}
	}
	
	var pitanje = "Potvrdite unos sledećih podataka:";
	pitanje += "\n\nKomitent:\n\t"+els["komitent"].value;
	pitanje += "\n\nPoreklo:\n\t"+els["poreklo"].options[els["poreklo"].selectedIndex].innerHTML;
	pitanje += "\n\nKontakt:\n\t"+els["kontakt"].options[els["kontakt"].selectedIndex].innerHTML;
	pitanje += "\n\nOdgovor:\n\t"+els["odgovor"].options[els["odgovor"].selectedIndex].innerHTML;
	if(els["spec_dogadjaj"].value)
		pitanje += "\n\nSpec.događaj:\n\t"+els["spec_dogadjaj"].value.replace(/\n/g,"\n\t");
	if(els["napomena"].value)
	pitanje += "\n\nNapomena:\n\t"+els["napomena"].value.replace(/\n/g,"\n\t");
	pitanje += "\n\nIshod:\n\t"+els["result"].options[els["result"].selectedIndex].innerHTML;
	pitanje += "\n\nVreme:\n\t"+els["result_date"].value+" "+els["result_hour"].value+":"+els["result_minute"].value;
	if(els["result_desc"].value)
		pitanje += "\n\nOpis:\n\t"+els["result_desc"].value.replace(/\n/g,"\n\t");
	
	if(confirm(pitanje)){
		LoadRSDoc("rokovnik.php","afterNew('"+els["komitent_id"].value+"','"+els["komitent"].value.replace("'","\\'")+"')",out,"",1);
	}
}
function afterNew(response,id,name){
	alert(response);
	setRokovnikKomitent(id,name,0,0);
}
function afterDeleteTMCall(response){
	var tmp = response.split("{!}",2);
	var code = parseInt(tmp[0]);
	var msg = tmp[1];
	alert(msg)
	if(!code){
		var forma = document.getElementById("reportRokovnikTelemarketing");
		var sbmt = forma.getElementsByTagName("input");
		sbmt = sbmt[sbmt.length-1];
		sbmt.click();
	}
}
var RokovnikTimeout=null;
function rokovnikVreme(){
	var rokovnikDateObject = document.getElementById("date");
	if(rokovnikDateObject){
		now = new Date();
		var day = now.getDate();
		var month = now.getMonth()+1;
		var year = now.getFullYear();
		var hour = now.getHours();
		var minute = now.getMinutes();
		var second = now.getSeconds();
		
		if(day<10){
			day='0'+day;
		}
		if(month<10){
			month='0'+month;
		}
		if(hour<10){
			hour='0'+hour;
		}
		if(minute<10){
			minute='0'+minute;
		}
		
		
		rokovnikDateObject.value = day+"."+month+"."+year+" "+hour+":"+minute;
		RokovnikTimeout = setTimeout("rokovnikVreme();",60000);
	}else{
		if(RokovnikTimeout){
			clearTimeout(RokovnikTimeout);
		}
	}
}
function searchKomitentRokovnik(btn,manager,level){
	if(typeof(level) == "undefined")level=0;
	var old = btn.previousSibling.previousSibling.previousSibling.previousSibling.value;
	var kw = prompt("Komitent?\nDeo naziva ili šifra:",trim(old));
	kw = trim(kw);
	if(kw){
		var action = "rokovnik.php?a=findKom&k="+escape(kw)+"&level="+level;
		if(manager){
			action+="&man="+manager;
		}
		if(level){
			LoadRSDoc(action,"ypAjaxForm(2)");
		}else{
			LoadRSDoc(action,"ypAjaxForm");
		}
	}
}
function showkomitentDetails(btn){
	var kom_id = btn.previousSibling.previousSibling.previousSibling.previousSibling.value;
	if(kom_id){
		LoadRSDoc("rokovnik.php?a=komDetails&id="+kom_id,"updateKomDetails")
	}
}
function updateKomDetails(response){
	document.getElementById("komitent_details").innerHTML = response;
}
function komitentMoreAddresses(id){
	LoadRSDoc('rokovnik.php?a=komeDetailsDelAddresses&id='+id,'afterKomitentMoreAddresses');
}
function afterKomitentMoreAddresses(response){
	if(response != 0){
		ypAjaxForm(response);
	}
}

function deleteRokovnikCall(id,ask){
	if(confirm('Potvrdite brisanje poziva: '+ask)){
		LoadRSDoc("rokovnik.php?a=adminCalls&mod=del&id="+id,"afterDelCall");
	}
}
function afterDelCall(response){
	alert(response);
	LoadRSDoc("rokovnik.php?a=adminCalls","setContent");
}
function saveRokovnikEditCall(forma){
	var els = forma.elements;
	var action = "a=adminCalls&mod=save";
	action += "&id="+els["call_id"].value;
	action += "&kom="+els["komitent_id"].value;
	action += "&por="+els["poreklo"].value;
	action += "&kon="+els["kontakt"].value;
	action += "&odg="+els["odgovor"].value;
	action += "&sdo="+els["spec_dogadjaj"].value;
	action += "&nap="+els["napomena"].value;
	action += "&rid="+els["result_id"].value;
	action += "&rda="+els["result_date"].value;
	action += "&rho="+els["result_hour"].value;
	action += "&rmi="+els["result_minute"].value;
	action += "&rde="+els["result_desc"].value;
	alert(action);
	LoadRSDoc("rokovnik.php","afterSaveRokovnikEdit",action,'',1);
}
function afterSaveRokovnikEdit(response){
	alert(response);
	closeYpAjaxForm();
	LoadRSDoc("rokovnik.php?a=adminCalls","setContent");
}
function setRokovnikKomitent(id,name,manager,level){
	if(level==1){
		var sl = document.getElementById("searchList");
		sl.parentNode.parentNode.removeChild(sl.parentNode);
		document.getElementById("komitent").value = name;
		document.getElementById("komitent_id").value = id;
	}else{
		var kom_id = document.getElementById("komitent_id");
		var kom = document.getElementById("komitent");
		kom.value = name;
		kom_id.value = id;
		LoadRSDoc("rokovnik.php?a=yapyap&id="+id,"updateYapYap");
		LoadRSDoc("rokovnik.php?a=kom&mod=bykomitent&id="+id,"updateKomercijalaContact");
		if(manager<1){
			var chobj = document.getElementById("history_details_content");
			var div = document.createElement("div");
			div.id = "throbber1";
			div.style.display = "block";
			div.innerHTML = "Učitavam...";
			chobj.appendChild(div);
			LoadRSDoc("rokovnik.php?a=komHistory&kom="+id+"&name="+name,'updateKomHistory');
			LoadRSDoc("rokovnik.php?a=komDetails&id="+id,"updateKomDetails");
		}
		kom_id.nextSibling.nextSibling.focus();
	}
}
function updateKomercijalaContact(response){
	var tmp = response.split("{!}",2);
	var code = tmp[0];
	var msg = tmp[1];
	
	var kcb = document.getElementById("komContactsBtn");
	var kct = document.getElementById("komContactsTxt");
	if(code>0){
		kcb.style.display='block';
		kct.value = msg;
	}else{
		kcb.style.display='none';
		kct.value = "";
	}
}
function updateYapYap(response){
	var ol = document.getElementById("yapyap");
	var YapYapBtn = document.getElementById("YapYapBtn");
	if(ol){
		ol = ol.getElementsByTagName("ol")[0];
		if(ol){
			ol.innerHTML = response;
			if(response.length){
				YapYapBtn.style.display="block";
			}else{
				YapYapBtn.style.display="none";
			}
		}else{
			alert("No list element");
		}
	}else{
		alert("No div element");
	}
}
function updateKomHistory(response){
	if(response){
		var tmp = response.split(/{!}/);
		var cn = tmp[0];
		var ch = tmp[1];
		var cu = tmp[2];
		
		var cnobj = document.getElementById("num_calls");
		var chobj = document.getElementById("history_details_content");
		var cuobj = document.getElementById("ugovor");
		
		if(cu){
			cuobj.onclick = function(){
				ypAjaxForm(cu);
			}
			cuobj.style.display = "block";
		}else{
			cuobj.onclick = "";
			cuobj.style.display = "none";
		}
	
		cnobj.innerHTML = cn;
		cnobj.style.display = 'block';
		chobj.innerHTML = "";
		if(ch){
			chobj.innerHTML = ch;
		}else{
			chobj.innerHTML = "";
		}
	}
}
function fixNumStepVal(obj){
	var v = trim(toNumber(obj.value));
	var maxval = parseInt(obj.getAttribute("maxval"));
	var minval = parseInt(obj.getAttribute("minval"));
	if(maxval && maxval<v){
		obj.value = maxval;
	}
	if(minval && minval>v){
		obj.value = minval;
	}
	obj.value = obj.value.replace(/[^\d]/g,"");
	
	if(!obj.value.length){
		obj.value = "0";
	}
}
function filterObavljenKontakt(q){
	var dest = q.nextSibling.nextSibling;
	var opts = dest.options;
	if(q.value>=0){
		for(i=0; i<opts.length; i++){
			var ob = opts[i].getAttribute("obavljen");
			if(ob==q.value){
				opts[i].style.display = "block";
				opts[i].selected = true;
			}else{
				opts[i].style.display = "none";
			}
		}
	}else{
		for(i=0; i<opts.length; i++){
			opts[i].style.display = "block";
		}
	}
}
function filterOvlascenOdgovor(q){
	var dest = q.nextSibling.nextSibling;
	var opts = dest.options;
	if(q.value>=0){
		for(i=0; i<opts.length; i++){
			var ob = opts[i].getAttribute("ovlascen");
			if(ob==q.value){
				opts[i].style.display = "block";
				opts[i].selected = true;
			}else{
				opts[i].style.display = "none";
			}
		}
	}else{
		for(i=0; i<opts.length; i++){
			opts[i].style.display = "block";
		}
	}
}
function rokovnikAddFeedback(forma,izmena){
	
	if(typeof(izmena)=="undefined"){
		izmena=0;
	}
	
	var els = forma.elements;
	if(!els["sastanak_id"].value){
		alert("Morate odabrati sastanak!");
		els["sastanak_id"].nextSibling.nextSibling.focus();
		return;
	}
	if(!els["sastanak_date"].value || (!els["sastanak_hour"].value && els["sastanak_hour"].value > 0)){
		alert("Morate odabrati datum i vreme održavanja sastanka!");
		els["sastanak_date"].focus();
		return;
	}
	var tmp = els["sastanak_date"].value.split('.',3);
	tmp[0] = (tmp[0].length<2)?(tmp[0]==0?"01":"0"+tmp[0]):tmp[0];
	tmp[1] = (tmp[1].length<2)?(tmp[1]==0?"01":"0"+tmp[1]):tmp[1];
	tmp[2] = (tmp[2].length<4)?(tmp[2].length<3)?(tmp[2].length<2?"200"+tmp[2]:"20"+tmp[2]):"1"+tmp[2]:tmp[2];
	
	els["sastanak_date"].value = tmp[0]+"."+tmp[1]+"."+tmp[2];
	els["sastanak_hour"].value = els["sastanak_hour"].value.length<2?"0"+els["sastanak_hour"].value:els["sastanak_hour"].value
	els["sastanak_minute"].value = els["sastanak_minute"].value.length<2?"0"+els["sastanak_minute"].value:els["sastanak_minute"].value
	
	var action = "rokovnik.php?a=addMeetingFeedback";
	action += "&izmena="+izmena;
	action += "&r="+els["radnik"].value;
	
	action += "&id="+els["sastanak_id"].value;
	if(izmena){
		action += "&theID="+els["the_sastanak"].value;
		action += "&unet_time="+els["unet_time"].value;
	}

	action += "&fb="+els["feedback"].value;
	action += "&date="+els["sastanak_date"].value;
	action += "&h="+els["sastanak_hour"].value;
	action += "&m="+els["sastanak_minute"].value;
	action += "&date1="+els["ishod_date"].value;
	action += "&h1="+els["ishod_hour"].value;
	action += "&m1="+els["ishod_minute"].value;
	action += "&n="+escape(trim(els["napomena"].value));
	
	pitanje = "Potvrdite unos sledećih podataka\n-------------------------------------------------\n".toUpperCase();
	pitanje += "Zakazan:\n\t- "+document.getElementById("zakazan_selected").innerHTML+"\n";
	pitanje += "Održan:\n\t";
	
	var radnik_ime = els["radnik"].options[els["radnik"].selectedIndex].innerHTML;
	radnik_ime = radnik_ime.replace("&nbsp;"," ");
	
	pitanje += "- Radnik: "+radnik_ime+"\n\t";
	pitanje += "- "+els["sastanak_date"].value+" "+els["sastanak_hour"].value+":"+els["sastanak_minute"].value+"\n\t";
	pitanje += "- "+els["feedback"].options[els["feedback"].selectedIndex].innerHTML+": "+els["ishod_date"].value+" "+els["ishod_hour"].value+":"+els["ishod_minute"].value;
	if(els["napomena"].value){
		pitanje += "\n\t- "+els["napomena"].value.replace(/\n/g,"\n\t  ");
	}
	
	if(confirm(pitanje)){
		LoadRSDoc(action,"afterAddFeedback("+izmena+")");
	}
}
function afterAddFeedback(response,izmena){
	alert(response);
	if(izmena){
		LoadRSDoc("rokovnik.php?a=adminMeetings","setContent");
	}else{
		LoadRSDoc("rokovnik.php?a=manager","setContent");
	}
}
function rokovnikDeleteMeeting(id){
	if(confirm("Potvrdite brisanje sastanka?")){
		LoadRSDoc("rokovnik.php?a=adminMeetings&mod=delete&id="+id,"afterDeleteMeeting");
	}
}
function afterDeleteMeeting(response){
	alert(response);
	LoadRSDoc("rokovnik.php?a=adminMeetings","setContent");
}
function rokovnikEditMeeting(id){
	LoadRSDoc("rokovnik.php?a=adminMeetings&mod=showEdit&id="+id,"setContent");
}
function findMeeting(btn){
	var komid = document.getElementsByName("komitent_id")[0];
	var kom = document.getElementsByName("komitent")[0];
	var sastanak = document.getElementById("sastanak_id");
	
	if(!komid.value){
		alert("Morate odabrati komitenta!");
		komid.nextSibling.nextSibling.focus();
		return;
	}
	var date = document.getElementsByName("result_date")[0];
	var hour = document.getElementsByName("result_hour")[0];
	var minute = document.getElementsByName("result_minute")[0];
	if(!date.value){
		alert("Morate upisati datum sastanka.");
		date.focus();
		return;
	}
	LoadRSDoc("rokovnik.php?a=searchSastanak&k="+kom.value+"&kom="+komid.value+"&d="+date.value+"&h="+hour.value+"&m="+minute.value,"ypAjaxForm");
}
function setMeeting(id,time){
	var sas = document.getElementById("sastanak_id");
	var zs = document.getElementById("zakazan_selected");
	if(!sas || !zs){
		alert("Error instancing element object to set sastanak!");
		return;
	}
	sas.value = id;
	sas.parentNode.style.display = "none";
	zs.innerHTML = time;
	zs.parentNode.style.display = 'block';
	document.getElementById("sastanak_date").focus();
}
function filterMeetings(forma){
	var els = forma.elements;
	var action = "a=adminMeetings";
	for(var i=0; i<els.length; i++){
		if(typeof(els[i].name) != "undefined" && els[i].name.length>0)
		action += "&"+els[i].name+"="+els[i].value;
	}
	LoadRSDoc("rokovnik.php",'setContent',action,'',1);
}
function rokovnikIstaVrednost(sourceObj,destID){
	var dest = document.getElementById(destID);
	if(dest){
		dest.value = sourceObj.value;
	}else{
		window.status = "No object with id '"+destID+"'";
	}
}
function alarmOnclick(a,type){
	
	var ol = a.nextSibling.nextSibling;
	var olstat = (ol.style.display=='block')?1:0;
	
	if(olstat){
		ol.style.display='none';
	}else{
		ol.style.display='block';
		var a = "a=doTheAlarm";
		if(type){
			a+="&mod="+type;
		}
		//window.status = a+typeof(type);
		LoadRSDoc("rokovnik.php?"+a,"updateAlarms");
	}
}
function rokovnikAlarms(){
	LoadRSDoc("rokovnik.php?a=doTheAlarm","updateAlarms");
	//alarmTO = setTimeout("rokovnikAlarms('"+tout+"')",tout);
}
function updateAlarms(response){
	var al = document.getElementById("rokovnik_alarms");
	var a = al.getElementsByTagName("input");
	a = a[0];
	var ul = al.getElementsByTagName("ol");
	ul = ul[0];
	if(response == 0){
		al.style.display = "none";
	}else{
		var tmp = response.split("{!}");
		var rest = tmp[0];
		var content = tmp[1];
		al.style.display = "block";
		a.value = rest;
		if(rest == 0){
			a.style.backgroundImage="url(gui/icons/alarm_still.gif)";
		}
		ul.innerHTML = content;
	}
}

function rokovnikReport(forma){
	var els = forma.elements;
	var action = "";
	for(var i=0; i<els.length; i++){
		if(els[i].name)
		action += "&"+els[i].name+"="+trim(els[i].value);
	}
	LoadRSDoc("rokovnik.php","setContent",action,'',1);
}

function addRokovnikKomerc(forma){
	els = forma.elements;
	
	if(trim(els["komitent_id"].value).length<1){
		alert("Niste odabrali komitenta.");
		els["komitent_id"].nextSibling.nextSibling.focus();
		return;
	}
	if(trim(els["visit_date"].value).length<1){
		alert("Niste popunili vreme posete.");
		els["visit_date"].nextSibling.nextSibling.focus();
		return;
	}
	if(trim(els["poreklo"].value)<1){
		alert("Niste odabrali poreklo komitenta.");
		els["poreklo"].focus();
		return;
	}
	var pitanje = "Potvrdite unos sledećih podataka:";
	pitanje += "\n\nKomitent: "+els["komitent"].value;

	var ime = (els["radnik"].value > 0)?els["radnik"].options[els["radnik"].selectedIndex].innerHTML.replace("&nbsp;"," "):"";
	pitanje += "\nKomercijalista: "+ime;
	pitanje += "\nVreme posete: "+els["visit_date"].value+" "+els["visit_hour"].value+":"+els["visit_minute"].value;
	
	var ter = (els["teritorija"].value > 0)?els["teritorija"].options[els["teritorija"].selectedIndex].innerHTML.replace("&nbsp;"," "):"";
	pitanje += "\nTeritorija: "+ter;
	
	pitanje += "\nVreme poziva: "+els["zak_call_date"].value+" "+els["zak_call_hour"].value+":"+els["zak_call_minute"].value;
	pitanje += "\nPoreklo: "+els["poreklo"].options[els["poreklo"].selectedIndex].innerHTML.replace("&nbsp;"," ");
	pitanje += "\nOdgovor: "+els["odgovor"].options[els["odgovor"].selectedIndex].innerHTML.replace("&nbsp;"," ");
	pitanje += "\nNapomena: "+els["napomena"].value.replace(/\n/g,"\n\t\t");
	
	if(!confirm(pitanje)){
		return;
	}
	
	var action = "";
	for(var i=0; i<els.length; i++){
		if(els[i].name){
			action += "&"+els[i].name+"="+escape(els[i].value);
		}
	}
	action = action.substr(1);
	LoadRSDoc("rokovnik.php","afterAddRokovnikKomerc",action,"",1);
}
function afterAddRokovnikKomerc(response){
	alert(response)
	LoadRSDoc("rokovnik.php?a=kom",'setContent');
}
function distribTeritorijeAdmin(forma){
	var datum = forma.elements["datum"];
	if(datum.value.match(/^\d{1,2}\.\d{1,2}\.\d{2,4}$/)){
		LoadRSDoc("rokovnik.php","setContent","a=distrib&mod=teritory&datum="+datum.value,"",1);
	}else{
		alert("Datum nije validan!\nUpišite datum u formatu: dd.mm.gggg");
	}
}
function showNewTeritory(datum){
	
	var html = '<form class="area" action="javascript:void(0)" method="post" onsubmit="addNewTeritory(this)" id="newTeritoryForm">';
	html += '<h2>Nova teritorija</h2>';
	html += '<label for="code_new">Ime:</label><input type="text" name="code_new" id="code_new" /><br clear="all" />';
	html += '<label for="datum_new">Datum:</label><input type="text" name="datum_new" id="datum_new" value="'+datum+'" /><input type="button" class="datePicker" onclick="displayDatePicker(\'datum_new\',this)" /><br clear="all" />';
	html += '<label for="desc_new">Opis:</label><textarea name="desc_new" id="desc_new"></textarea><br clear="all" />';
	html += '<input type="submit" value="sačuvaj" /></form>';
	
	ypAjaxForm(html);
}
function addNewTeritory(forma,edit){
	var els = forma.elements;
	if(trim(els["code_new"].value).length<1){
		alert("Unesite naziv teritorije");
		return;
	}
	if(trim(els["datum_new"].value).length<1){
		alert("Unesite datum važenja teritorije");
		return;
	}
	
	if(!trim(els["datum_new"].value).match(/^\d{1,2}\.\d{1,2}\.\d{2,4}$/)){
		alert("Datum važenja teritorije nije validan.\nUnesite ga u formatu: dd.mm.gggg");
		return;
	}
	if(!edit){
		var action = "a=distrib&mod=newTeritory";
	}else{
		var action = "a=distrib&mod=saveTeritory";
		action += "&id="+els["id"].value;
	}
	action += "&name="+trim(els["code_new"].value);
	action += "&datum="+trim(els["datum_new"].value);
	action += "&desc="+escape(trim(els["desc_new"].value));
	
	LoadRSDoc("rokovnik.php","afterNewTeritory('"+trim(els["datum_new"].value)+"')",action,"",1);
}
function afterNewTeritory(response,datum){
	alert(response);
	closeYpAjaxForm();
	LoadRSDoc("rokovnik.php","setContent","a=distrib&mod=teritory&datum="+datum,"",1);
}
function editTeritorija(btn){
	var tr = btn.parentNode.parentNode;
	var id = tr.id.substr(0,tr.id.indexOf("_"));
	var tds = tr.getElementsByTagName("td");
	var code = trim(tds[0].innerHTML);
	var desc = trim(tds[1].innerHTML);
	var date = document.getElementById("datum").value;
	
	var html = '<form class="area" action="javascript:void(0)" method="post" onsubmit="addNewTeritory(this,1)" id="saveTeritoryForm">';
	html += '<h2>Izmena Teritorije</h2>';
	html += '<input type="hidden" name="id" value="'+id+'" />';
	html += '<label for="code_new">Ime:</label><input type="text" name="code_new" id="code_new" value="'+code+'" /><br clear="all" />';
	html += '<label for="datum_new">Datum:</label><input type="text" name="datum_new" id="datum_new" value="'+date+'" /><input type="button" class="datePicker" onclick="displayDatePicker(\'datum_new\',this)" /><br clear="all" />';
	html += '<label for="desc_new">Opis:</label><textarea name="desc_new" id="desc_new">'+desc+'</textarea><br clear="all" />';
	html += '<input type="submit" value="sačuvaj" /></form>';
	
	ypAjaxForm(html);
}
function deleteTeritorija(btn){
	var tr = btn.parentNode.parentNode;
	var id = tr.id.substr(0,tr.id.indexOf("_"));
	var date = trim(document.getElementById("datum").value);
	
	LoadRSDoc("rokovnik.php?a=distrib&mod=delTeritory&id="+id,"afterEditDeleteTeritorija('"+date+"')");
}
function afterEditDeleteTeritorija(response,date){
	alert(response);
	if(document.getElementById("darker")){
		closeYpAjaxForm();
	}
	LoadRSDoc("rokovnik.php","setContent","a=distrib&mod=teritory&datum="+date,"",1);
}
function setTeritorySelect(inpt){
	var dateStart = inpt.value;
	var okDate = dateStart.match(/^\s*\d{1,2}\.\d{1,2}\.\d{2,4}\s*$/);

	var sel = document.getElementById("teritorija");
	
	var opts = sel.getElementsByTagName("optgroup");
	var index=false;
	for(var i=1; i<opts.length; i++){
		var date = opts[i].getAttribute("date");
		if(date==dateStart || !okDate){
			if(index === false){
				index = i;
			}
			opts[i].style.display="block";
			opts[i].disabled = false;
		}else{
			opts[i].style.display="none";
			opts[i].disabled = true;
		}
	}
	if(index !== false){
		opts[index].childNodes[0].selected = true;
	}
	
}
function showTeritorijaDates(sel){
	var dates = sel.options[sel.selectedIndex].getAttribute("dates");
	var dsel = document.getElementById('td');//sel.nextSibling.nextSibling;
	var teritorije = document.getElementById("teritorije");
	teritorije.value = sel.options[sel.selectedIndex].getAttribute("ids");
	ter = teritorije.value.split(",");
	if(dsel.options.length>1){
		for(var i=1; i<dsel.options.length; i++){
			dsel.removeChild(dsel.options[i]);
		}
	}
	if(dates){
		var d = dates.split(",");
		for(i=0; i<d.length; i++){
			var tmp = d[i].split("-",3);
			var datum = tmp[2]+"."+tmp[1]+"."+tmp[0];
			var opt = new Option(datum,datum);
			opt.setAttribute("ter_id",ter[i]);
			dsel.options[i+1] = opt;
		}
	}
}
function updateTeritorije(sel){
	var datum_obj = document.getElementById('datum');
	var ter_obj = document.getElementById('teritorije');
	var ter1_obj = document.getElementById("tt");
	
	ter_obj.value=sel.options[sel.selectedIndex].getAttribute('ter_id');
	if(sel.value!=-1){
		datum_obj.value=sel.value;
	}else{
		datum_obj.value="";
		if(ter1_obj.value){
			ter_obj.value = ter1_obj.options[ter1_obj.selectedIndex].getAttribute("ids");
		}
	}

}
var tr=null
function showMoreInfo(komid,t){
	LoadRSDoc("rokovnik.php?a=report&t=tmMore&id="+komid,"afterMoreInfo");
	var trs = t.parentNode.getElementsByTagName("tr");
	for(var i=1; i<trs.length; i++){
		trs[i].className = "notSelected";
	}
	tr = t;
}
function afterMoreInfo(response){
	tr.className = "selected";
	document.getElementById("rokovnikMoreInfo").innerHTML = response;
}
function exportNepozvaniKomitenti(btn){
	var tbl = btn.nextSibling.nextSibling;
	if(tbl.nodeName.toLowerCase() == "table"){
		var trs = tbl.getElementsByTagName("tr");
		var ids = Array();
		for(var i=1; i<trs.length; i++){
			var td = trs[i].getElementsByTagName("td");
			var id = trim(td[0].innerHTML);
			var name = trim(td[1].innerHTML);
			ids.push(id+"<=>"+name);
		}
		var ids = implode(",",ids);
		ids = escape(ids);
		var action = "a=report&t=kom1&ids="+ids;
		LoadRSDoc("rokovnik.php","afterExportNepozvaniKomitenti",action,"",1);
	}
}
function afterExportNepozvaniKomitenti(response){
	var html = '<div class="area">';
	html += '<a class="download" href="down.php/?f='+response+'&fn=nepozvani_komitenti.xls" target="_blank" onclick="closeYpAjaxForm()">preuzmite fajl</a>';
	html += '</div>'
	ypAjaxForm(html);
}
function createMeetingTicket(btn){
	var komid = parseInt(document.getElementById("komitent_id").value);
	if(komid>0){
		var mdate = document.getElementById("result_date").value;
		var mhour = document.getElementById("result_hour").value;
		var mminute = document.getElementById("result_minute").value;
		var kontakt = document.getElementById("kontakt").value;
		var nap = document.getElementById("result_desc").value;
		var poreklo = document.getElementById("poreklo");
		var poreklo_name = poreklo.options[poreklo.selectedIndex].innerHTML;
		var poreklo = poreklo.value;
		
		var action = "a=adminMeetings&mod=newMeetingTicket";
		action += "&komid="+komid;
		action += "&mD="+mdate;
		action += "&mH="+mhour;
		action += "&mM="+mminute;
		action += "&napomena="+escape(nap);
		action += "&kontakt="+kontakt;
		action += "&poreklo="+poreklo;
		action += "&poreklo_name="+poreklo_name;
		LoadRSDoc("rokovnik.php","ypAjaxForm",action,"",1);
	}else{
		alert("Niste odabrali komitenta");
		return;
	}
}
function afterSaveTicket(response){
	alert(response);
	closeYpAjaxForm();
	
	var form = document.getElementById("filterForm");
	if(form){
		var inpts = form.getElementsByTagName("input");
		var subm=null;
		for(var i=0; i<inpts.length; i++){
			if(inpts[i].type.toLowerCase()=="submit"){
				subm = inpts[i];
				break;
			}
		}
		subm.click();
	}
}
function ticketsSelectAll(a){
	var ol = document.getElementById("MTResultList");
	var lis = ol.getElementsByTagName("li");
	var check;
	var num=0;
	for(var i=0; i<lis.length; i++){
		var inpt = lis[i].getElementsByTagName("input");
		if(inpt.length){
			inpt = inpt[0];
			if(i==0){
				check = inpt.checked?false:true;
			}
			if((inpt.style.display!='none') && check==true){
				num++;
				lis[i].className="selected"
			}else{
				lis[i].className="";
			}
			inpt.checked = check;
		}
	}
	a.innerHTML = check?"nijedan ["+num+"]":"svi";
}
function ticketSelectDeselect(li){
	var inpt = li.getElementsByTagName("input")[0];
	if(inpt.style.display!="none"){
		li.className = li.className=="selected"?"":"selected";
		inpt.checked = inpt.checked?false:true;
	}
}
function createMeetingTickets(btn,txt){
	if(typeof(txt)=="undefined"){
		txt=0;
	}
	
	var ol = document.getElementById("MTResultList");
	var lis = ol.getElementsByTagName("li");
	var ids="";
	for(var i=0; i<lis.length; i++){
		var inpt = lis[i].getElementsByTagName("input");
		if(inpt.length){
			inpt = inpt[0];
			if(inpt.checked==true && inpt.style.display!='none'){
				ids += ";"+lis[i].getAttribute("value");
			}
		}
	}
	if(ids.length){
		ids = ids.substr(1);
		var mod = (txt==1)?"exportMeetingTickets":"generateMeetingTickets";
		var af  = (txt==1)?"afterExport":"afterCreateMeetingTickets";
		LoadRSDoc("rokovnik.php?a=adminMeetings&mod="+mod+"&ids="+ids,af);
	}else{
		var msg = (txt==1)?"Odaberite listiće koje želite da izvezete.":"Odaberite listiće koje želite da odštampate.";
		alert(msg);
	}
}
function afterCreateMeetingTickets(response,izvoz){
	var div = document.getElementById("MT_listici");
	var inpt = div.getElementsByTagName("input")[0];
	var ol = document.getElementById("MT_listici_list");
	ol.innerHTML = response;
	if(izvoz){
		inpt.style.display='none';
	}else{
		inpt.style.display='block';
	}
	div.style.display="block";
}
function meetingTicketsAddkomercijalista(btn,id){
	LoadRSDoc("rokovnik.php?a=adminMeetings&mod=editTicket&id="+id,"ypAjaxForm");
}
function meetingTicketsDelete(btn,id,user){
	var html = '<div class="area">';
	html += '<form method="POST" action="javascript:void(0);" onsubmit="submitForm(this,\'rokovnik.php\',\'POST\',\'afterDeleteTicket\',1)">';
	html += '<label for="pass">Lozinka menadžera sa listića:</label>';
	html += '<input type="hidden" name="a" value="adminMeetings" />';
	html += '<input type="hidden" name="mod" value="deleteTicket" />';
	html += '<input type="hidden" name="id" value="'+id+'" />';
	html += '<input type="hidden" name="userCreated" value="'+user+'" />';
	html += '<input type="password" name="pass" id="pass" />';
	html += '<input type="submit" value="obriši" />';
	html += '</form>';
	html += "</div>";
	
	ypAjaxForm(html);
}
function afterDeleteTicket(response){
	alert(response);
	closeYpAjaxForm();
	
	var form = document.getElementById("filterForm");
	if(form){
		var inpts = form.getElementsByTagName("input");
		var subm=null;
		for(var i=0; i<inpts.length; i++){
			if(inpts[i].type.toLowerCase()=="submit"){
				subm = inpts[i];
				break;
			}
		}
		subm.click();
	}
}
function afterAddKomercijalistaMT(response){
	alert(response);
	closeYpAjaxForm();
}